//
// Created by 24087 on 2025/4/27.
// 接口层中，缓存区设计 V1.0 仅采用普通数组，先实现算法功能
//		   后续缓存区预计改为循环队列


#ifndef PARSER_H
#define PARSER_H

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

#include "Interface_Motor.h"
#include "Queue1.h"


extern Queue1 PairPool;

/*常量配置*/
enum
{
	BUFFER_SIZE = 8,
	Motor_NUM = 6
};

/*ROS数据对*/
typedef struct ROS_Pair
{
	uint8_t data_H[BUFFER_SIZE];
	uint8_t data_L[BUFFER_SIZE];
	uint32_t uniqueID;
	bool isComplete; // 标记是否已经完成配对
} ROS_Pair;


typedef enum
{
	HAL_OK1 = 0x00U, // 操作成功
	HAL_ERROR1 = 0x01U, // 操作失败
	HAL_BUSY1 = 0x02U, // 设备或资源忙
	HAL_TIMEOUT1 = 0x03U // 操作超时
} HAL_StatusTypeDef1;

/*帧配置*/
typedef enum
{
	/*ROS帧*/
	ROS_FrameSize = 8,
	ROS_FrameHeader = 0x05,
	ROS_FrameTail_H = 0xFF,
	ROS_FrameTail_L = 0xFE

	/*Motor帧*/
} Frame_Config;

typedef struct
{
	uint8_t dir;
	uint16_t vel;
	uint8_t acc;
	uint32_t clk; /*3200个脉冲为电机1圈*/
} TargetInfo;

extern const uint8_t data_H[8];
extern const uint8_t data_L[8];

/*缓存区搬运器*/
void BufferMov_ROS(const uint8_t *data, Queue1 *PairPool);

/*信号检查器*/
HAL_StatusTypeDef1 ROS_Check(const uint8_t *data);

/*pair缓存区初始化*/
void Pairpool_Init(Queue1 *PairPool);

/*pair销毁*/
void Pairpool_Destroy(Queue1 *PairPool);

/*pair判空*/
bool Pairpool_Empty(const Queue1 *PairPool);

/*pair入队*/
void Pairpool_Push(Queue1 *PairPool, const uint32_t ID, const uint8_t *rawdata, const bool isHighByte);

/*pair出队*/
QE_DataType1 Pairpool_Pop(Queue1 *PairPool);

/*ROS信号发送*/
void ROS_Send(TargetInfo *targetInfo);

/*ROS时间统一器*/
TargetInfo *ROS_TimeUnify(float *targetAngle, const MotorInfo *MotorPool);

/*信号解码器*/
float *Parser_ROS(const uint8_t *data_H, const uint8_t *data_L);

/*ROS信号处理*/
void ROS_Process(Queue1 *PairPool, const MotorInfo *MotorPool);


#endif //PARSER_H
